lock

Purpose

The lock method obtains a pessimistic lock using an SQL select … for update.

Examples

def book = Book.get(1)
book.lock()

Description

The lock method will obtain a pessimistic lock on an instance, essentially locking a row in the database with select … for update. The lock method is equivalent to using Hibernate's LockMode.UPGRADE in combination with the lock method.

The lock will automatically be released when the transaction is committed. In Grails this is typically after an action has finished executing.

Refer to the section on Optimistic and Pessimistic locking in the user guide for info.